输出聊天信息 搜索动作关键字 运行动作动画
//输出聊天信息 搜索动作关键字 运行动作动画
using UnityEngine;
using System.Collections;
public class Msg : MonoBehaviour {
string stringToEdit=""; //输出的聊天信息
Hashtable actionMap = new Hashtable();
string Str_action="";
string Str_input="";
// string regex = "///w{1.5}/s"; //正则表达式
public GUISkin guiskin;
// Use this for initialization
ArrayList oneWordActionList = new ArrayList();
ArrayList twoWordActionList = new ArrayList();
ArrayList threeWordActionList = new ArrayList();
ArrayList fourWordActionList = new ArrayList();
void Start () {
//~ // action map initialization
//~ ArrayList oneWordActionList = new ArrayList();
//~ ArrayList twoWordActionList = new ArrayList();
//~ ArrayList threeWordActionList = new ArrayList();
//~ ArrayList fourWordActionList = new ArrayList();
//add actions
//oneWordActionList.Add("/闲");
//oneWordActionList.Add("/跑");
//oneWordActionList.Add("/坐");
oneWordActionList.Add("/汗");
oneWordActionList.Add("/哭");
oneWordActionList.Add("/嘘");
oneWordActionList.Add("/晕");
actionMap.Add("1",oneWordActionList);
twoWordActionList.Add("/回头");
twoWordActionList.Add("/叉腰");
twoWordActionList.Add("/自恋");
twoWordActionList.Add("/偷笑");
twoWordActionList.Add("/哼哼");
twoWordActionList.Add("/生气");
twoWordActionList.Add("/害羞");
twoWordActionList.Add("/夸奖");
twoWordActionList.Add("/得意");
twoWordActionList.Add("/鼓掌");
twoWordActionList.Add("/大笑");
twoWordActionList.Add("/天啊");
twoWordActionList.Add("/装死");
twoWordActionList.Add("/飞吻");
twoWordActionList.Add("/没有");
twoWordActionList.Add("/不要");
twoWordActionList.Add("/过来");
twoWordActionList.Add("/鞠躬");
twoWordActionList.Add("/再见");
actionMap.Add("2",twoWordActionList);
threeWordActionList.Add("/打招呼");
actionMap.Add("3",threeWordActionList);
fourWordActionList.Add("/汗流浃背");
actionMap.Add("4″,fourWordActionList);
}
void Update () {
}
void OnGUI()
{
GUI.skin=guiskin;
stringToEdit = GUI.TextField (new Rect (10, 10, 200, 30), stringToEdit);
if(GUI.Button(new Rect (100, 40, 80, 30),"发送"))
{
Str_input=findaction(stringToEdit);
}
GUI.Button(new Rect (200, 60, 100, 30),Str_action);
GUI.Label(new Rect (200, 90, 400, 30),Str_input);
}
string findaction(string msg)
{
/////////////如果有斜杠并且有空格才运行///////////////////
//~ if(msg.Contains("/")&&msg.Contains(" "))
//~ {
//~ //运行
//~ }
/////////////////////////////////?//////////////////?/////
int msglength=msg.Length; //string的长度
int sprit_subcript; //斜杠的下标
string compare=""; //用来比较的字符串
bool deletestr=false; //是否删除截取出来的字符串
//遍历斜杠
for(int j=0;j<msg.Length;j++)
{
if(msg[j]==’/’)
{
sprit_subcript=j;
int sprit_length=msglength-sprit_subcript; //string的长度减去斜杠的下标
/******判断防止越界**********/
if(sprit_length>=6)
{
//////////////
if(msg[sprit_subcript+2]==’ ‘)
{
//oneWordActionList
ArrayList list = (ArrayList) actionMap["1"];
//~ print("list:"+list.Count);
string[] arrString = (string[])list.ToArray(typeof(string));
//~ print(arrString.Length);
compare=stringToEdit.Substring(sprit_subcript,2);
//~ print("__"+stringToEdit.Substring(sprit_subcript,3));//比较
for(int i=0;i<arrString.Length;i++)
{
if(compare==arrString[i])
{
oneWordActionRun(i);
deletestr=true;
}
}
//~ Str_action=arrString[2];
}
else if(msg[sprit_subcript+3]==’ ‘)
{
//twoWordActionList
ArrayList list = (ArrayList) actionMap["2"];
string[] arrString = (string[])list.ToArray(typeof(string));
compare=stringToEdit.Substring(sprit_subcript,3);
for(int i=0;i<arrString.Length;i++)
{
if(compare==arrString[i])
{
twoWordActionRun(i);
deletestr=true;
}
}
//~ print("__"+stringToEdit.Substring(sprit_subcript,3));//比较
}
else if(msg[sprit_subcript+4]==’ ‘)
{
//threeWordActionList
ArrayList list = (ArrayList) actionMap["3"];
string[] arrString = (string[])list.ToArray(typeof(string));
compare=stringToEdit.Substring(sprit_subcript,4);
for(int i=0;i<arrString.Length;i++)
{
if(compare==arrString[i])
{
threeWordActionRun(i);
deletestr=true;
}
}
}
else if(msg[sprit_subcript+5]==’ ‘)
{
//fourWordActionList
ArrayList list = (ArrayList) actionMap["4"];
string[] arrString = (string[])list.ToArray(typeof(string));
compare=stringToEdit.Substring(sprit_subcript,5);
for(int i=0;i<arrString.Length;i++)
{
if(compare==arrString[i])
{
fourWordActionRun(i);
deletestr=true;
}
}
//~ print("__"+stringToEdit.Substring(sprit_subcript,5));//比较
}
///////////////////
}
else if(sprit_length>=5)
{
/////////////
if(msg[sprit_subcript+2]==’ ‘)
{
//oneWordActionList
ArrayList list = (ArrayList) actionMap["1"];
//~ print("list:"+list.Count);
string[] arrString = (string[])list.ToArray(typeof(string));
//~ print(arrString.Length);
compare=stringToEdit.Substring(sprit_subcript,2);
//~ print("__"+stringToEdit.Substring(sprit_subcript,3));//比较
for(int i=0;i<arrString.Length;i++)
{
if(compare==arrString[i])
{
oneWordActionRun(i);
deletestr=true;
}
}
//~ Str_action=arrString[2];
}
else if(msg[sprit_subcript+3]==’ ‘)
{
//twoWordActionList
ArrayList list = (ArrayList) actionMap["2"];
string[] arrString = (string[])list.ToArray(typeof(string));
compare=stringToEdit.Substring(sprit_subcript,3);
for(int i=0;i<arrString.Length;i++)
{
if(compare==arrString[i])
{
twoWordActionRun(i);
deletestr=true;
}
}
//~ print("__"+stringToEdit.Substring(sprit_subcript,3));//比较
}
else if(msg[sprit_subcript+4]==’ ‘)
{
//threeWordActionList
ArrayList list = (ArrayList) actionMap["3"];
string[] arrString = (string[])list.ToArray(typeof(string));
compare=stringToEdit.Substring(sprit_subcript,4);
for(int i=0;i<arrString.Length;i++)
{
if(compare==arrString[i])
{
threeWordActionRun(i);
deletestr=true;
}
}
}
///////////////////
}
else if(sprit_length>=4)
{
////
if(msg[sprit_subcript+2]==’ ‘)
{
//oneWordActionList
ArrayList list = (ArrayList) actionMap["1"];
//~ print("list:"+list.Count);
string[] arrString = (string[])list.ToArray(typeof(string));
//~ print(arrString.Length);
compare=stringToEdit.Substring(sprit_subcript,2);
//~ print("__"+stringToEdit.Substring(sprit_subcript,3));//比较
for(int i=0;i<arrString.Length;i++)
{
if(compare==arrString[i])
{
oneWordActionRun(i);
deletestr=true;
}
}
//~ Str_action=arrString[2];
}
else if(msg[sprit_subcript+3]==’ ‘)
{
//twoWordActionList
ArrayList list = (ArrayList) actionMap["2"];
string[] arrString = (string[])list.ToArray(typeof(string));
compare=stringToEdit.Substring(sprit_subcript,3);
for(int i=0;i<arrString.Length;i++)
{
if(compare==arrString[i])
{
twoWordActionRun(i);
deletestr=true;
}
}
//~ print("__"+stringToEdit.Substring(sprit_subcript,3));//比较
}
////
}
else if(sprit_length>=3)
{
//////
if(msg[sprit_subcript+2]==’ ‘)
{
//oneWordActionList
ArrayList list = (ArrayList) actionMap["1″];
string[] arrString = (string[])list.ToArray(typeof(string));
compare=stringToEdit.Substring(sprit_subcript,2);
for(int i=0;i<arrString.Length;i++)
{
if(compare==arrString[i])
{
oneWordActionRun(i);
deletestr=true;
}
}
//~ Str_action=arrString[2];
}
//////
}
/****************************/
}
}
if(deletestr)
msg=msg.Replace(compare+’ ‘,"");
return msg;
/////////正则表达式
//~ string[] actionArray = msg.match(regex);
//~ if(actionArray!=null && actionArray.Length>0){
//~ string tempAction = null;
//~ int actionStringLength = 0;
//~ for(int i=0;i<actionArray.Length;i++){
//~ tempAction = actionArray[i];
//~ actionStringLength = tempAction.Length;
//~ ArrayList actionList = null;
//~ actionList = actionMap.get(new string(actionStringLength));
//~ if(actionList!= null && actionList.contains(tempAction.trim())){
//~ Str_action = tempAction.trim();
//~ }
//~ }
//~ }
}
void oneWordActionRun(int i)
{
switch (i)
{
case 0:
Str_action="汗";
break;
case 1:
Str_action="哭";
break;
case 2:
Str_action="嘘";
break;
case 3:
Str_action="晕";
break;
}
}
void twoWordActionRun(int i)
{
switch (i)
{
case 0:
Str_action="回头";
break;
case 1:
Str_action="叉腰";
break;
case 2:
Str_action="自恋";
break;
case 3:
Str_action="偷笑";
break;
case 4:
Str_action="哼哼";
break;
case 5:
Str_action="生气";
break;
case 6:
Str_action="害羞";
break;
case 7:
Str_action="夸奖";
break;
case 8:
Str_action="得意";
break;
case 9:
Str_action="鼓掌";
break;
case 10:
Str_action="大笑";
break;
case 11:
Str_action="天啊";
break;
case 12:
Str_action="飞吻";
break;
case 13:
Str_action="没有";
break;
case 14:
Str_action="不要";
break;
case 15:
Str_action="过来";
break;
case 16:
Str_action="鞠躬";
break;
case 17:
Str_action="再见";
break;
}
}
void threeWordActionRun(int i)
{
switch (i)
{
case 0:
Str_action="打招呼";
break;
}
}
void fourWordActionRun(int i)
{
switch (i)
{
case 0:
Str_action="汗流浃背";
break;
}
}
}
本条目发布于2010年5月28日。属于Unity脚本分类。